<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class ProductRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * validation rules for user
     *
     * @return array
     */
    public function rules()
    {
        $id = $this->route('product');
        if($this->isMethod('post')){
            return [
                'name' => 'required|unique:products',
                'category_id' => 'required|exists:categories,id',
                'unit' => 'required|max:255',
                'notes' => 'max:255'
            ];
        }else{
            return [
                'name' => 'required|unique:products,name,'.$id,
                'category_id' => 'required|exists:categories,id',
                'unit' => 'required|max:255',
                'notes' => 'max:255'
            ];
        }
    }

    /**
     * Get the error messages for the defined validation rules.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'name.required' => _("请输入物料名称"),
            'name.unique'  => _("物料名称已存在"),
            'category_id.required' => _("请选择物料分类"),
            'category_id.exists' => _("物料分类不存在"),
            'unit.required' => _("请填写单位")
        ];
    }
}
